package HomeWork20241202;

//import java.io.*;
//
//public class HomeWork20241202 {
//    public static void main(String args[]) {
//        File file = new File("file.txt");
//        String content[] = {"天气预报:", "北京晴", "天津多云,有小雨", "长沙晴转多云"};
//        try {
//            FileWriter outOne = new FileWriter(file);
//            BufferedWriter outTwo = new BufferedWriter(outOne); // 【代码1】创建BufferedWriter对象
//
//            for (String str : content) {
//                outTwo.write(str); // 【代码2】写入字符串到文件
//                outTwo.newLine(); // 添加换行
//            }
//            outTwo.close();
//            outOne.close(); // 关闭FileWriter
//
//            FileReader inOne = new FileReader(file); // 【代码3】创建FileReader对象
//            BufferedReader inTwo = new BufferedReader(inOne);
//            String s = null;
//            while ((s = inTwo.readLine()) != null) { // 【代码4】读取文件内容直到文件末尾
//                System.out.println(s);
//            }
//            inOne.close();
//            inTwo.close();
//        } catch (IOException e) { // 【代码5】捕获IOException
//            System.out.println(e);
//        }
//    }
//}

//import java.io.*;
//import java.util.Scanner;
//
//public class HomeWork20241202 {
//    public static void main(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("Enter the string to encrypt:");
//        String originalString = scanner.nextLine();
//        scanner.close();
//
//        // 加密字符串
//        String encryptedString = encrypt(originalString);
////        System.out.println(encryptedString);
//
//        // 将加密后的字符串写入文件
//        File file = new File("encrypted.txt");
//        try {
//            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
//            writer.write(encryptedString);
//            writer.close();
//        } catch (IOException e) {
//            System.out.println("An error occurred while writing to the file.");
//            e.printStackTrace();
//            return;
//        }
//
//        // 从文件中读取加密后的字符串
//        try {
//            BufferedReader reader = new BufferedReader(new FileReader(file));
//            String encryptedFromFile = reader.readLine();
//            reader.close();
//
//            // 解密字符串并显示
//            String decryptedString = decrypt(encryptedFromFile);
//            System.out.println("Decrypted string from file:");
//            System.out.println(decryptedString);
//        } catch (IOException e) {
//            System.out.println("An error occurred while reading from the file.");
//            e.printStackTrace();
//        }
//    }
//
//    // 简单的加密方法：将每个字符的ASCII值加1
//    private static String encrypt(String text) {
//        StringBuilder encrypted = new StringBuilder();
//        for (char character : text.toCharArray()) {
//            encrypted.append((char) (character + 1));
//        }
//        return encrypted.toString();
//    }
//
//    // 简单的解密方法：将每个字符的ASCII值减1
//    private static String decrypt(String text) {
//        StringBuilder decrypted = new StringBuilder();
//        for (char character : text.toCharArray()) {
//            decrypted.append((char) (character - 1));
//        }
//        return decrypted.toString();
//    }
//}

import java.io.*;

public class HomeWork20241202 {
    public static void main(String[] args) {
        // 设置源文件和目标文件路径
        String sourceFile = "D:\\MyCode\\JavaCode\\java-code-\\SchoolHomeWork\\HomeWork\\src\\HomeWork20241202\\HomeWork20241202.java";
        String targetFile = "temp.txt";

        // 使用File对象表示源文件和目标文件
        File inputFile = new File(sourceFile);
        File outputFile = new File(targetFile);

        // 创建字符输入流和字符输出流
        try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {

            // 读取源文件内容并写入目标文件
            String line;
            while ((line = reader.readLine()) != null) {
                writer.write(line);
                writer.newLine(); // 添加换行符以保持文件格式
            }

            System.out.println("File copied successfully.");

        } catch (IOException e) {
            // 处理可能发生的IOException
            System.out.println("An error occurred while copying the file.");
            e.printStackTrace();
        }
    }
}